import sys
import os
import threading
import time
import logging
from flask import Flask, render_template
from router.module import module_bp

# log = logging.getLogger('werkzeug')
# log.setLevel(logging.ERROR)

app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False

# app.register_blueprint(host_bp)
app.register_blueprint(module_bp)
# app.register_blueprint(download_bp)


# response.headers['X-Frame-Options'] = 'SAMEORIGIN'
# add X-Frame-Options to response headers for secure
@app.after_request
def apply_caching(response):
    response.headers["X-Frame-Options"] = "SAMEORIGIN"
    return response


def start_browser(tname, delay):
    time.sleep(delay)
    os.system("..\\GoogleChromePortable\\App\\Chrome-bin\\chrome.exe --disable-extensions --app=http://127.0.0.1:5000")
    # os.system("..\\electron\\electron.exe http://127.0.0.1:5000")
    

def start_server(tname, delay):
    time.sleep(delay)
    app.run(host='127.0.0.1', port=5000, debug=False, threaded=True)

if __name__ == '__main__':
    print("  ___    _____    ____     ____     ___    ____    _  _  ")
    print(" / _ \  |  ___|  / ___|   |___ \   / _ \  |___ \  | || | ")
    print("| | | | | |_    | |         __) | | | | |   __) | | || |_")
    print("| |_| | |  _|   | |___     / __/  | |_| |  / __/  |__   _")
    print(" \___/  |_|      \____|   |_____|  \___/  |_____|    |_| ")
    print("                                                         ")  
    try:
        t1 = threading.Thread(target=start_server, args=('t2_server', 1,))
        t2 = threading.Thread(target=start_browser, args=('t1_browser', 3,))
        t1.start()
        t2.start()
        # t1.join()
        # t2.join()
    except:
        print("Error: unable to start thread")
